package com.niit.cms.controller;

import com.niit.cms.entity.*;
import com.niit.cms.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: cao_jiaqi
 * @CreateTime： 2022/7/2 16:29
 */
@Controller
public class StudyController {
    @Resource
    private MyCourseService myCourseService;
    @Resource
    private CourseService courseService;
    @Resource
    private CatalogService catalogService;
    @Resource
    private VideoService videoService;
    @Resource
    private ProgressService progressService;
    /**
     * @Description: 我的课程页面
     * @Author: cao_jiaqi 
     * @CreateTime: 2022/7/2 20:39 
     * @param: model
     * @param: session 
     * @return: java.lang.String
     **/
    @GetMapping("/study")
    public String index(Model model, HttpSession session, @RequestParam(name = "state", required = false, defaultValue = "2") Integer state){
        Userinfo userinfo = (Userinfo) session.getAttribute("user");
        Integer uid  = userinfo.getId();
        List<MyCourse> myCourseList = null;
        if(state == 0){
            myCourseList = myCourseService.queryListByUidState(uid, state);
            calculate(myCourseList);
            
        }else if(state == 1){
            myCourseList = myCourseService.queryListByUidState(uid, state);
            calculate(myCourseList);
        }else{
            myCourseList = myCourseService.queryListByUid(uid);
            calculate(myCourseList);
        }
        model.addAttribute("myCourseList", myCourseList);
        return "mycourse";
    }
    /**
     * @Description: 课程播放页面
     * @Author: cao_jiaqi 
     * @CreateTime: 2022/7/2 20:41 
     * @param: model
     * @param: uid
     * @param: courseId 
     * @return: java.lang.String
     **/
    @GetMapping("/studyDetail")
    public String studyDetail(Model model, @RequestParam("uid") Integer uid, @RequestParam("courseId") Integer courseId){
        Course course = courseService.queryById(courseId);
        model.addAttribute("course", course);
        //获取课程目录
        List<Catalog> catalogList = catalogService.queryListByCourseId(courseId);
        //获取课程视频
        for(Catalog catalog : catalogList){
            catalog.setVlist(videoService.queryListByCatalogId(catalog.getId()));
        }
        model.addAttribute("catalogList", catalogList);
        model.addAttribute("video",catalogList.get(0).getVlist().get(0));
        return "study_detail";
    }

    /**
     * @Description: 计算课程进度并更新到数据库中
     * @Author: cao_jiaqi
     * @CreateTime: 2022/7/6 13:23
     **/
    public void calculate(List<MyCourse> list){
        for(MyCourse myCourse : list){
            int sum = 0, finish = 0;
            double courseProcess = 0.00;
            Integer courseId = myCourse.getCourseId();
            //该课程下所有目录
            List<Catalog> catalogList = catalogService.queryListByCourseId(courseId);
            //System.out.println("catalogList=" + catalogList);
            if(catalogList != null && catalogList.size() != 0){
                //所有视频
                List<Video> vlist = new ArrayList<>();
                for(Catalog catalog : catalogList){
                    List<Video> videoList = videoService.queryListByCatalogId(catalog.getId());
                    //System.out.println("videoList=" + videoList);
                    if(videoList != null && videoList.size() != 0){
                        for(Video video : videoList){
                            vlist.add(video);
                        }
                    }
                }
                if(vlist != null && vlist.size() != 0){
                    //该课程总时长
                    //System.out.println("vList=" + vlist);
                    for(Video video : vlist){
                        sum += video.getDuration().getSecond() + (video.getDuration().getMinute()) * 60 + (video.getDuration().getHour()) * 3600;
                    }
                    System.out.println(sum);
                }
                //已经完成了的视频的总时长
                List<Progress> progressList = progressService.queryListByCourseIdState(courseId, 1);
                //System.out.println("progressList=" + progressList);
                if(progressList != null && progressList.size() != 0){
                    for(Progress progress : progressList){
                        //该课程已经完成了的视频
                        Video video = videoService.queryById(progress.getVid());
                        finish += video.getDuration().getSecond() + (video.getDuration().getMinute()) * 60 + (video.getDuration().getHour()) * 3600;
                    }
                    System.out.println(finish);
                }
                if(sum != 0) {
                    courseProcess = finish / (sum * 1.00);
                    DecimalFormat df = new DecimalFormat("0.00");
                    myCourse.setCourseProcess(Double.parseDouble(df.format(courseProcess)));
                    myCourseService.update(myCourse);
                }
            }

        }

    }

}
